﻿Imports System.IO
Imports System.Drawing.Imaging
Public Class MainForm
    Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "PNG Files (*.png)|*.Png"
        ofd.Multiselect = True
        If (ofd.ShowDialog() <> Windows.Forms.DialogResult.OK) Then Return

        For Each file As String In ofd.FileNames
            Dim index As Integer = Me.FileList.Items.Add(New CustomListItem(System.IO.Path.GetFileName(file), file))
            Me.FileList.SetItemChecked(index, True)
        Next
    End Sub
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If Me.FileList.Items.Count > 0 Then
            For i As Integer = 0 To Me.FileList.Items.Count - 1
                Me.FileList.SetItemChecked(i, Me.CheckBox1.Checked)
            Next
        End If
    End Sub
    Private Sub FileList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileList.SelectedIndexChanged
        If FileList.SelectedIndex > -1 Then
            Dim img As Image = Image.FromFile(DirectCast(FileList.SelectedItem, CustomListItem).FullPath)
            If Not img Is Nothing Then
                Me.picPreview.Image = img.GetThumbnailImage(60, 60, Nothing, New IntPtr())
                Me.lblheight.Text = String.Format("{0} x {1}", img.Height, img.Width)
            End If
            img.Dispose()
        End If
    End Sub
    Private Sub cmdConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvert.Click

        If Me.FileList.CheckedItems.Count = 0 Then
            MessageBox.Show("Please select PNG files to convert.")
            Return
        End If

        Try

            lblProgress.Text = ""
            pbar.Value = 0

            Dim fsd As New FolderBrowserDialog()

            If fsd.ShowDialog() <> Windows.Forms.DialogResult.OK Then
                Return
            End If

            Dim total As Integer = Me.FileList.CheckedItems.Count
            pbar.Maximum = total
            Application.DoEvents()

            Dim jgpEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)

            ' Create an Encoder object based on the GUID
            ' for the Quality parameter category.
            Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
            Dim myEncoderParameters As New EncoderParameters(1)
            Dim myEncoderParameter As New EncoderParameter(Encoder.Quality, CType(JpgQuality.Value, Int32))
            myEncoderParameters.Param(0) = myEncoderParameter

            Dim count As Integer
            For Each item As CustomListItem In Me.FileList.CheckedItems

                Dim fileName As String = Path.Combine(fsd.SelectedPath, Path.GetFileNameWithoutExtension(item.FullPath) & ".Jpg")
                'best resize option found with A4 document size
                'Dim img As Image = System.Drawing.Image.FromFile(item.FullPath)

                If (File.Exists(fileName)) Then
                    File.Copy(fileName, fileName & ".bak")
                End If

                Dim img As New Bitmap(item.FullPath)
                img.Save(fileName, jgpEncoder, myEncoderParameters)
                img.Dispose()
                img = Nothing
                count += 1
                pbar.Value = count
                pbar.Refresh()
                lblProgress.Text = String.Format("Processing {0} of {1}", count, total)
                Application.DoEvents()
            Next

            lblProgress.Text = "Writing to PDF file"
            lblProgress.Refresh()
            Threading.Thread.Sleep(10)


            lblProgress.Text = "Completed ..."
            Threading.Thread.Sleep(10)

            'If MessageBox.Show("Do you want to open the generated PDF file?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            '    System.Diagnostics.Process.Start(fsd.FileName)
            'End If


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo

        Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageDecoders()

        Dim codec As ImageCodecInfo
        For Each codec In codecs
            If codec.FormatID = format.Guid Then
                Return codec
            End If
        Next codec
        Return Nothing

    End Function
End Class
